package android.test.devicetest;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.KeyEvent;
import android.net.Uri;
import android.test.getintent.GetIntent;

public class VideoPlay extends Activity implements SurfaceHolder.Callback {
    private static final String LOG_TAG = "VideoPlayTest";

    private MediaPlayer   mplayer = null;
    private SurfaceView   mview   = null;
    private SurfaceHolder mholder = null;
    private Context       mcontext= this;
    private AudioManager mAudioManager;
    private int curVolume;
    private int maxVolume;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.video);
        mcontext = this;
        mview    = (SurfaceView)findViewById(R.id.video_view);
        mholder  = mview.getHolder();
        mholder.addCallback(this);

        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        curVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mplayer = new MediaPlayer();
            mplayer.setDataSource("/system/media/video.avi");
            mplayer.setDisplay(mholder);
            mplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mplayer.setScreenOnWhilePlaying(true);
            mplayer.prepare();
            mplayer.start();
            mplayer.setLooping(true);
        } catch (Exception e) {
            Log.e(LOG_TAG, e.toString());
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if (mplayer != null) mplayer.release();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            mplayer.release();
            finish();
            getIntents("recorder");
            break;

        case KeyEvent.KEYCODE_BACK:
            mplayer.release();
            finish();
            System.exit(0);
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            curVolume++;
            if (curVolume > maxVolume) {
                curVolume = maxVolume;
            }
            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            curVolume--;
            if (curVolume < 0) {
                curVolume = 0;
            }
            break;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume, 0);
        return super.onKeyDown(keyCode, event);
    }

    private void getIntents(String intent) {
        Intent mIntent = GetIntent.getItemIntent(this, intent);
        if (mIntent != null) {
            startActivity(mIntent);
        }
    }
}

